from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import random

# 设置请求头
options = Options()
options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")

# 启动浏览器
driver = webdriver.Chrome(options=options)
driver.set_window_size(1200, 900)

# 访问目标URL
url = "https://search.jd.com/Search?keyword=海南慧谷"
driver.get(url)

# 方法3：设置页面编码为UTF-8
driver.execute_script("document.charset='utf-8';")

# 方法4：模拟正常用户行为
# 随机等待时间，模拟用户浏览行为
time.sleep(random.uniform(2, 5))

# 滚动页面，触发动态加载
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(random.uniform(2, 5))  # 等待页面滚动加载完成

# 方法1：显式等待，确保页面加载完成
try:
    # 等待价格元素加载完成
    WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.CSS_SELECTOR, "div.p-price i"))
    )
except Exception as e:
    print("价格元素加载超时或未找到。")
    print(f"错误信息: {e}")

# 方法2：检查CSS选择器并提取数据
try:
    # 等待页面加载完成
    time.sleep(5)
    items = driver.find_elements(By.CSS_SELECTOR, "li.gl-item")

    for item in items:
        # 提取价格
        try:
            price_element = item.find_element(By.CSS_SELECTOR, "div.p-price i")
            price = price_element.text.strip()
            if not price or price == "??":
                print("价格提取失败，可能是选择器不准确或页面未加载完成。")
                price = "价格未知"
        except Exception as e:
            print("价格提取失败。")
            print(f"错误信息: {e}")
            price = "价格未知"

        # 提取链接
        try:
            url = item.find_element(By.CSS_SELECTOR, "div.p-img a").get_attribute("href")
        except Exception as e:
            print("链接提取失败。")
            print(f"错误信息: {e}")
            url = "链接未知"

        # 提取标题
        try:
            title = item.find_element(By.CSS_SELECTOR, "div.p-name em").text.strip()
        except Exception as e:
            print("标题提取失败。")
            print(f"错误信息: {e}")
            title = "标题未知"

        # 打印结果
        print(f"价格: {price}")
        print(f"链接: {url}")
        print(f"标题: {title}")
        print("-" * 30)

except Exception as e:
    print("未找到目标结构，请检查网页内容或选择器是否正确。")
    print(f"错误信息: {e}")

finally:
    driver.quit()  # 关闭浏览器